package com.zygk.locationtool.dao;

import android.content.Context;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;
import com.yanzhenjie.nohttp.rest.StringRequest;
import com.zygk.library.util.CallServer;
import com.zygk.library.util.JsonUtil;
import com.zygk.library.util.ToastUtil;
import com.zygk.locationtool.Application;
import com.zygk.locationtool.config.Urls;
import com.zygk.locationtool.model.M_Station;
import com.zygk.locationtool.model.apimodel.APIM_StationList;
import com.zygk.locationtool.model.apimodel.APIM_TownList;
import com.zygk.locationtool.model.apimodel.CommonResult;
import com.zygk.locationtool.util.HttpRequest;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Wikison on 2018/12/10.
 */
public class StationLogic {
  /**
   * 镇站点列表
   */
  public static void getTownStationList(final Context mContext, int State,
      final HttpRequest.HttpCallback callback) {
    StringRequest request =
        new StringRequest(Application.getInstance().API_URL + Urls.GetTownStationList,
            RequestMethod.POST);
    JSONObject object = new JSONObject();
    try {
      object.put("Type", State);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    request.setDefineRequestBodyForJson(object);

    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_TownList result = JsonUtil.jsonToObject(response.get(), APIM_TownList.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getMessage());
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 根据搜索词获取站点列表
   */
  public static void getSearchStationList(final Context mContext, String StationName,
      final HttpRequest.HttpCallback callback) {
    StringRequest request =
        new StringRequest(Application.getInstance().API_URL + Urls.GetSearchStationList,
            RequestMethod.POST);
    JSONObject object = new JSONObject();
    try {
      object.put("StationName", StationName);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    request.setDefineRequestBodyForJson(object);

    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_StationList result = JsonUtil.jsonToObject(response.get(), APIM_StationList.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getMessage());
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 保存站点经纬度
   */
  public static void saveStationLocation(final Context mContext, M_Station station,
      final HttpRequest.HttpCallback callback) {
    StringRequest request =
        new StringRequest(Application.getInstance().API_URL + Urls.SaveStationLocation,
            RequestMethod.POST);
    JSONObject object = new JSONObject();
    try {
      object.put("StationID", station.getStationID());
      object.put("Location", station.getLocation());
      object.put("Location_BMap", station.getLocation_BMap());
      object.put("Location_AMap", station.getLocation_AMap());
    } catch (JSONException e) {
      e.printStackTrace();
    }
    request.setDefineRequestBodyForJson(object);

    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getMessage());
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }
}
